﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using MTS;
using System.IO;

namespace MTSGui
{
    /// <summary>
    /// Interaction logic for Options.xaml
    /// </summary>
    public partial class Options : Window
    {
        private bool postLoad;

        public Options()
        {
            postLoad = false;

            InitializeComponent();

            postLoad = true;

            BlockColors colorer = new BlockColors();
            Dictionary<int, Blocks> blocks = colorer.Blocks;
            foreach (KeyValuePair<int, Blocks> kvp in blocks)
            {
                if(kvp.Value.Id != -1)
                    blockSelect.Items.Add(kvp.Value);
            }

            Properties.Settings.Default.Reload();
            this.lightingLevel = Properties.Settings.Default.LightLevel;
            this.imageType = (ImageType)System.Enum.Parse(typeof(ImageType), Properties.Settings.Default.RenderMode);
            this.spectrographBlock = Properties.Settings.Default.spectrographBlock;
        }

        private void buttonComplete_Click(object sender, RoutedEventArgs e)
        {
            //Save settings
            Properties.Settings.Default.RenderMode = System.Enum.Format(typeof(ImageType), imageType, "g");
            Properties.Settings.Default.LightLevel = this.lightingLevel;
            Properties.Settings.Default.spectrographBlock = this.spectrographBlock;

            Properties.Settings.Default.Save();
            this.DialogResult = true;
            this.Close();
        }

        private void lightingSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            LightingValue.Content = LightingSlider.Value;
        }

        public ImageType imageType
        {
            get
            {
                if ((Boolean)this.radioOblique.IsChecked)
                {
                    return ImageType.OBLIQUE;
                }
                else if ((Boolean)this.radioTerrain.IsChecked)
                {
                    return ImageType.TERRAIN;
                }
                else if ((Boolean)this.radioHeightmap.IsChecked)
                {
                    return ImageType.HEIGHTMAP;
                }
                else if ((Boolean)this.radioCavemap.IsChecked)
                {
                    return ImageType.CAVEMAP;
                }
                else if ((Boolean)this.radioSpectrograph.IsChecked)
                {
                    return ImageType.SPECTROGRAPH;
                }
                else
                {
                    throw new Exception("No image type selected");
                }
            }
            set
            {
                if (value.Equals(ImageType.OBLIQUE))
                {
                    this.radioOblique.IsChecked = true;
                }
                else if (value.Equals(ImageType.TERRAIN))
                {
                    this.radioTerrain.IsChecked = true;
                }
                else if (value.Equals(ImageType.HEIGHTMAP))
                {
                    this.radioHeightmap.IsChecked = true;
                }
                else if (value.Equals(ImageType.CAVEMAP))
                {
                    this.radioCavemap.IsChecked = true;
                }
                else if (value.Equals(ImageType.SPECTROGRAPH))
                {
                    this.radioSpectrograph.IsChecked = true;
                }
            }
        }

        public byte lightingLevel
        {
            get
            {
                return (byte)(this.LightingSlider.Value - 1);
            }
            set
            {
                this.LightingSlider.Value = value + 1;
            }
        }

        public int spectrographBlock
        {
            get
            {
                Blocks block = (Blocks)blockSelect.SelectedItem;
                return block.Id;
            }
            set
            {
                foreach (Blocks block in blockSelect.Items)
                {
                    if (block.Id == value)
                    {
                        blockSelect.SelectedItem = block;
                    }
                }
            }
        }

        private void ModeChanged(object sender, RoutedEventArgs e)
        {
            if (!postLoad)
                return;

                ExtrasOblique.Visibility = (bool)radioOblique.IsChecked ? Visibility.Visible : Visibility.Collapsed;
                
                ExtrasSpectro.Visibility = (bool)radioSpectrograph.IsChecked ? Visibility.Visible : Visibility.Collapsed;

                ExtrasMisc.Visibility = ((bool)radioTerrain.IsChecked || (bool)radioHeightmap.IsChecked || (bool)radioCavemap.IsChecked) ? Visibility.Visible : Visibility.Collapsed;
        }
    }
}
